//
// Created by zhaog on 30/03/17.
//

#include <Poco/Stopwatch.h>
#include "pic_store_interface.h"
namespace pic_store_api {
    void PicStoreInterface::Init(string foshan_ip, string jinzhong_ip, string xianning_ip, string yancheng_ip) {
        _foshan_ip=foshan_ip;
        _jinzhong_ip=jinzhong_ip;
        _xianning_ip=xianning_ip;
        _yancheng_ip=yancheng_ip;
    }
    int PicStoreInterface::GetImg(vector<ImgStruct>& imgs){
        int total_count ;
        try {
            boost::shared_ptr<TSocket> foshan_socket;
            boost::shared_ptr<TTransport> foshan_transport;
            boost::shared_ptr<TProtocol> foshan_protocol;

            boost::shared_ptr<TSocket> xianning_socket;
            boost::shared_ptr<TTransport> xianning_transport;
            boost::shared_ptr<TProtocol> xianning_protocol;

            boost::shared_ptr<TSocket> jinzhong_socket;
            boost::shared_ptr<TTransport> jinzhong_transport;
            boost::shared_ptr<TProtocol> jinzhong_protocol;

            boost::shared_ptr<TSocket> yancheng_socket;
            boost::shared_ptr<TTransport> yancheng_transport;
            boost::shared_ptr<TProtocol> yancheng_protocol;

            foshan_socket = boost::shared_ptr<TSocket>(new TSocket(_foshan_ip, 2090));
            foshan_transport = boost::shared_ptr<TTransport>(new TFramedTransport(foshan_socket));
            foshan_protocol = boost::shared_ptr<TProtocol>(new TBinaryProtocol(foshan_transport));

            jinzhong_socket = boost::shared_ptr<TSocket>(new TSocket(_jinzhong_ip, 2090));
            jinzhong_transport = boost::shared_ptr<TTransport>(new TFramedTransport(jinzhong_socket));
            jinzhong_protocol = boost::shared_ptr<TProtocol>(new TBinaryProtocol(jinzhong_transport));

            xianning_socket = boost::shared_ptr<TSocket>(new TSocket(_xianning_ip, 2090));
            xianning_transport = boost::shared_ptr<TTransport>(new TFramedTransport(xianning_socket));
            xianning_protocol = boost::shared_ptr<TProtocol>(new TBinaryProtocol(xianning_transport));

            yancheng_socket = boost::shared_ptr<TSocket>(new TSocket(_yancheng_ip, 2090));
            yancheng_transport = boost::shared_ptr<TTransport>(new TFramedTransport(yancheng_socket));
            yancheng_protocol = boost::shared_ptr<TProtocol>(new TBinaryProtocol(yancheng_transport));

            std::map<string, string> foshan_file_map;
            std::map<string, string> jinzhong_file_map;
            std::map<string, string> xianning_file_map;
            std::map<string, string> yancheng_file_map;
            int foshan_req_count = 0, jinzhong_req_count = 0, xianning_req_count = 0, yancheng_req_count = 0;
            std::set<pic_store::get_param> foshan_set;
            std::set<pic_store::get_param> xianning_set;
            std::set<pic_store::get_param> jinzhong_set;
            std::set<pic_store::get_param> yancheng_set;
            PatchGetParams(imgs, foshan_set, xianning_set, jinzhong_set, yancheng_set);
            Poco::Stopwatch sw;
            sw.start();
            try {
                if (foshan_set.size() != 0) {
                    printf("before foshan %d\n",(int)sw.elapsed()/1000);
                    foshan_transport->open();
                    boost::shared_ptr<pic_store::psSvrClient> foshan_client;
                    foshan_client = boost::shared_ptr<pic_store::psSvrClient>(
                            new pic_store::psSvrClient(foshan_protocol));
                    foshan_client->get(foshan_file_map, foshan_set);
                    foshan_transport->close();
                    foshan_req_count = (int) foshan_file_map.size();
                    printf("after foshan %d count %d\n",(int)sw.elapsed()/1000,foshan_req_count);
                }
            }catch(...){
                return -1;
            }
            try {
                if (xianning_set.size() != 0) {

                    printf("before xianning %d\n",(int)sw.elapsed()/1000);
                    boost::shared_ptr<pic_store::psSvrClient> xianning_client;
                    xianning_transport->open();
                    xianning_client = boost::shared_ptr<pic_store::psSvrClient>(
                            new pic_store::psSvrClient(xianning_protocol));
                    xianning_client->get(xianning_file_map, xianning_set);
                    xianning_transport->close();
                    xianning_req_count = (int) xianning_file_map.size();
                    printf("after xianning %d count %d\n",(int)sw.elapsed()/1000,xianning_req_count);
                }
            }catch(...){
                return -1;
            }
            try {
                if (jinzhong_set.size() != 0) {
                    printf("before jinzhong %d\n",(int)sw.elapsed()/1000);
                    boost::shared_ptr<pic_store::psSvrClient> jinzhong_client;
                    jinzhong_transport->open();
                    jinzhong_client = boost::shared_ptr<pic_store::psSvrClient>(
                            new pic_store::psSvrClient(jinzhong_protocol));
                    jinzhong_client->get(jinzhong_file_map, jinzhong_set);
                    jinzhong_transport->close();
                    jinzhong_req_count = (int) jinzhong_file_map.size();
                    printf("after jinzhong %d count %d\n",(int)sw.elapsed()/1000,jinzhong_req_count);
                }
            }catch(...){
                return -1;
            }
            try {
                if (yancheng_set.size() != 0) {
                    printf("before yancheng %d\n",(int)sw.elapsed()/1000);
                    boost::shared_ptr<pic_store::psSvrClient> yancheng_client;
                    yancheng_transport->open();
                    yancheng_client = boost::shared_ptr<pic_store::psSvrClient>(
                            new pic_store::psSvrClient(yancheng_protocol));
                    yancheng_client->get(yancheng_file_map, yancheng_set);
                    yancheng_transport->close();
                    yancheng_req_count = (int) yancheng_file_map.size();
                    printf("after yancheng %d count %d\n",(int)sw.elapsed()/1000,yancheng_req_count);
                }
            }catch(...){
                return -1;
            }
            total_count=foshan_req_count + jinzhong_req_count + xianning_req_count + yancheng_req_count;
            WriteImgData(imgs, foshan_file_map);
            WriteImgData(imgs, jinzhong_file_map);
            WriteImgData(imgs, xianning_file_map);
            WriteImgData(imgs, yancheng_file_map);
        }catch(...){
            return -1;
        }
        return total_count;
    }

    int PicStoreInterface::PatchGetParams(const vector<ImgStruct> &meta_info, set<pic_store::get_param> &foshan,
                                    set<pic_store::get_param> &xianning, set<pic_store::get_param> &jinzhong,
                                    set<pic_store::get_param> &yancheng) {
        if(meta_info.size()==0)
            return -1;
        for(vector<ImgStruct>::const_iterator it=meta_info.begin();it!=meta_info.end();it++){
                pic_store::get_param param;
                param.uid=it->uid;
                if(it->label=="0"||it->label=="3"||it->label=="5"){
                    param.is035=true;
                }else{
                    param.is035=false;
                }
                string room_num;
                string img_name=it->img_name;
                if(img_name.size()==52){
                    room_num=img_name.substr(46,2);
                    param.hiido_serial=img_name.substr(0,45);
                }else{
                    continue;
                }
                if(room_num=="01"){
                    jinzhong.insert(param);
                }else if(room_num=="02"){
                    xianning.insert(param);
                }else if(room_num=="03"){
                    foshan.insert(param);
                }else if(room_num=="04"){
                    yancheng.insert(param);
                }else {
                    continue;
                }
            }
    }
    int PicStoreInterface::WriteImgData(vector<ImgStruct>& imgs, map<string, string> &guid_data) {
        int count=0;
        for(vector<ImgStruct>::iterator img_it=imgs.begin();img_it!=imgs.end();img_it++) {
                string file_name=img_it->img_name.substr(0,45);
                map<string,string>::iterator it=guid_data.find(file_name);
                if(it!=guid_data.end()){
                    img_it->file_data=it->second;
                    count++;
                }
        }
        return count;
    }
}